//
//  QDSelectComlicationController.m
//  simulateQuickDoctor
//
//  Created by 梁家伟 on 17/3/25.
//  Copyright © 2017年 PipiXia. All rights reserved.
//

#import "QDSelectComlicationController.h"
#import "QDComplicationCell.h"
#import "QDNetworkTool.h"
#import "QDComplicationModel.h"
#import "QDBannerTools.h"

@interface QDSelectComlicationController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,weak)UITableView* complicationList;
@end

@implementation QDSelectComlicationController{
    NSMutableArray<QDComplicationModel*>* _complicationsData;
    NSMutableArray<QDComplicationModel*>* _selectedComplications;
}

static NSString* complicationCell = @"complications";

- (instancetype)init{
    
    if(self = [super init]){
        _complicationsData = [NSMutableArray array];
        _selectedComplications =  [NSMutableArray array];
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self loadComplicationData];
}


- (void)saveComplications:(UIBarButtonItem*)sender{
    
//    NSLog(@"%@",_selectedComplications);
    self.callBackBlock(_selectedComplications);
    
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)setupUI{
    
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"确认" style:UIBarButtonItemStylePlain target:self action:@selector(saveComplications:)];
    
    
    //设置并发症选择列表
    UITableView* complicationList = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
    [self.view addSubview:complicationList];
    
    _complicationList = complicationList;
    
    complicationList.delegate = self;
    complicationList.dataSource = self;
    
    [complicationList mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    [complicationList registerClass:[QDComplicationCell class] forCellReuseIdentifier:complicationCell];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
   return _complicationsData.count;
}


- (QDComplicationCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    QDComplicationCell* cell = [tableView dequeueReusableCellWithIdentifier:complicationCell forIndexPath:indexPath];
    cell.model = _complicationsData[indexPath.row];
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    QDComplicationCell* cell = [tableView cellForRowAtIndexPath:indexPath];
    
    QDComplicationModel* model = cell.model;
    
    model.isSelected = !cell.model.isSelected;
    
    cell.model = model;
    
    if(model.isSelected){
        [_selectedComplications addObject:model];
    }else{
        [_selectedComplications removeObject:model];
    }
    
}


- (void)loadComplicationData{
    
    NSString* urlString = @"http://iosapi.itcast.cn/doctor/complicationList.json.php";
    
    NSDictionary* postParams = @{@"page":@1,@"page_size":@15,@"ci1_id":@(_ci1_id),@"ci2_id":@(_ci2_id)};
    
    QDNetworkTool* tool = [QDNetworkTool sharedTool];
    
    [QDBannerTools pp_BannerTitle:@"正在加载.." andTime:0.3];
    
    [tool requestWithHTTPMethod:@"POST" URLString:urlString parameters:postParams CallBack:^(id responseObj) {
        
        if(responseObj != nil){
            
            NSString* result = responseObj[@"msg"]; //FAIL
            
            if([result isEqualToString:@"OK"]){
                
                NSArray* dataArr = responseObj[@"data"];

                NSArray* modelArr = [NSArray yy_modelArrayWithClass:[QDComplicationModel class] json:dataArr];
                
                [_complicationsData addObjectsFromArray:modelArr];
                
                
                [_complicationList reloadData];
                
            }else{
                NSLog(@"请求参数错误");
            }
            
        }else{
            NSLog(@"网络请求错误");
        }
        
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
